Core.Link("StyleSheet", Core.GetUrl("Themes/Default/Management.css"), "text/css");

var Controls = null;
var Window = null, Control = null;

function init(completeCallback, errorCallback) {
    function LoadModulesComplete() {
        Controls = Core.GetModule("Controls.js");

        Control = Core.GetModule("Controls.js").Control;

        _init(completeCallback, errorCallback);
    }

    Core.LoadModules(
        LoadModulesComplete, errorCallback,
        ["Controls.js"]
    );
}

function _init(completeCallback, errorCallback) {
    try {
        //初始化代码，初始化完成后必须调用completeCallback;
        Core.LoadModules(function () {
            Common = Core.GetModule("Common.js");
            completeCallback();
        },
        errorCallback, ["Common.js"]);
    }
    catch (ex) {
        errorCallback(new Core.Exception(ex.mame, ex.message));
    }
}



function dispose(completeCallback, errorCallback) {
    _dispose(completeCallback, errorCallback);
}

function _dispose(completeCallback, errorCallback) {
    try {
        //卸载代码，卸载完成后必须调用completeCallback;
        completeCallback();
    }
    catch (ex) {
        errorCallback(new Core.Exception(ex.mame, ex.message));
    }
}

//共享全局变量和函数，在此定义的变量和函数将由该应用程序的所有实例共享
var Common = null;

function EmptyCallback() {

}

function Match(reg, str) {
    reg.lastIndex = 0;
    var ft = reg.exec(str);
    return (ft != null && ft.length == 1 && ft[0] == str)
}

function CheckEMail(email) {
    return Match(/[a-zA-Z0-9._\-]+@[a-zA-Z0-9._\-]+/ig, email);
}

function CheckTel(tel) {
    return Match(/[0-9\-]{6,30}/ig, tel);
}

function CheckMobile(mobile) {
    return Match(/[0-9]{11,11}/ig, mobile);
}

function CheckName(name) {
    return Match(/[0-9a-zA-Z]+/ig, name);
}

function CheckPassword(pwd) {
    return Match(/[0-9a-zA-Z]{4,10}/ig, pwd);
}

function GetHeadIMG(info, size, gred) {
    if (info.HeadIMG == "") {
        var url = Core.Config.ServiceUrl + "/images/HeadIMG/user"
        if (size > 0) url += "." + size;
        if (gred) url += ".gred";
        url += ".png";
        return url;
    }
    else {
        return String.format("{0}?user={1}&size={2}&gred={3}&headimg={4}", Core.GetPageUrl("headimg.aspx"), info.Name, size, gred, info.HeadIMG);
    }
}


Module.FriendPanel = FriendPanel;

function FriendPanelDS(config) {
    var obj = this;
    if (config == undefined) config = {};
    if (config.ShowState == undefined) config.ShowState = false;

    obj.GetSubNodes = function (callback, node) {
        if (node == null) {
            var nodes = [{
                Name: "Users",
                Text: "联系人",
                Tag: null,
                ImageCss: "Image16_Folder"
            }];
            callback(nodes);
        }
        else {
            var name = node.GetName();
            if (name != "Users" && name != "Groups") return null;

            var type = (name == "Users" ? 0 : 1);

            Common.GetFriends(function (friends) {
                var nodes = [];
                for (var k in friends) {
                    var friend = friends[k];
                    if (friend.Type == type) {
                        if (type == 0) {
                            nodes.push({
                                Name: friend.Name.toUpperCase(),
                                Text: friend.Nickname,
                                Tag: friend,
                                HasChildren: false,
                                ImageSrc: GetHeadIMG(friend, 16, config.ShowState && friend.State == "Offline")
                            });
                        }
                        else {
                            nodes.push({
                                Name: friend.Name.toUpperCase(),
                                Text: friend.Nickname,
                                Tag: friend,
                                HasChildren: false,
                                ImageCss: "Image16_Group"
                            });
                        }
                    }
                }
                callback(nodes);
            });
        }
    }

    return obj;
}

function FriendPanel(config) {
    var This = this;
    var OwnerForm = this;

    config.Css = "FriendPanel";
    config.DataSource = new FriendPanelDS(config.DSConfig);

    var width = config.Width, height = config.Height;
    config.Width = 243;
    config.Height = 620;

    Controls.TreeView.call(This, config);

    var Base = {
        GetType: This.GetType,
        is: This.is
    };

    This.GetType = function () { return "FriendPanel"; }
    This.is = function (type) { return type == This.GetType() ? true : Base.is(type); }

    This.Resize(width, height);

    This.OnDblClick.Attach(
    function () {
        if (This.GetSelectedNode().GetTag() != null) {
            var form = Core.Session.GetGlobal("ChatService").Open(This.GetSelectedNode().GetTag().Name, false);
        }
    });

}


Module.FriendForm = FriendForm;

function FriendForm(config) {
    var This = this;
    var OwnerForm = this;



    var width = config.Width, height = config.Height;
    config.Width = 233;
    config.Height = 707;

    Control.call(This, config);

    var Base = {
        GetType: This.GetType,
        is: This.is
    };

    This.GetType = function () { return "FriendForm"; }
    This.is = function (type) { return type == This.GetType() ? true : Base.is(type); }

    var m_Toolbar = new Controls.Toolbar({ "Left": 2, "Top": 2, "Width": 229, "Height": 24, "AnchorStyle": Controls.AnchorStyle.Left | Controls.AnchorStyle.Right | Controls.AnchorStyle.Top, "Parent": This, "Text": "功能菜单", "Css": "toolbar", "Items": [{ "Text": "功能菜单", "Css": "Image22_Add", "Command": "Option"}] });



    m_Toolbar.OnCommand.Attach(
        function (command) {
            if (command == "Option") {
                var toolbar_btn_dom = m_Toolbar.GetControl(0);
                var coord = Core.Utility.GetClientCoord(toolbar_btn_dom);
                m_Menu.Popup(coord.X, coord.Y + toolbar_btn_dom.offsetHeight + 2);
            }
        }
    )


    var m_FriendPanel_Config = { "Left": 2, "Top": 27, "Width": 229, "Height": 679, "AnchorStyle": Controls.AnchorStyle.Left | Controls.AnchorStyle.Right | Controls.AnchorStyle.Top | Controls.AnchorStyle.Bottom, "Parent": This, "Text": "", "Css": "control" };

    m_FriendPanel_Config.BorderWidth = 1;
    m_FriendPanel_Config.DSConfig = {
        ShowState: true
    };

    var m_FriendPanel = new FriendPanel(m_FriendPanel_Config);



    This.Resize(width, height);

    m_FriendPanel.Refresh(function () {
        m_FriendPanel.Expand(EmptyCallback, "/Users");
    });

    var menuConfig = {
        Items: [{
        Text: "消息管理",
        ID: "MsgHistory"
    }]
};

var m_Menu = new Controls.Menu(menuConfig);
m_Menu.OnCommand.Attach(
    function (command) {
        if (command == "MsgHistory") {
            Core.Session.GetGlobal("SingletonForm").ShowMsgHistoryForm();
        }
    });

CurrentWindow.OnNotify.Attach(
    function (command, data) {
        if (command == "FriendInfoChanged") {
            m_FriendPanel.Refresh(function () {
                m_FriendPanel.Expand(EmptyCallback, "/Users");
            });
        }
        else if (command == "UserInfoChanged") {
            CurrentWindow.SetTitle(Core.Session.GetUserInfo().Nickname);
        }
        else if (command == "UserStateChanged") {
            m_FriendPanel.GetAllNodes(function (node) {
                if (node.GetTag() != null && node.GetTag().ID == data.User) {
                    node.SetImage(GetHeadIMG(node.GetTag(), 16, data.State == "Offline"));
                }
            });
        }
    });

}


Module.AddFriendForm = AddFriendForm;

function AddFriendForm(config) {
    var This = this;
    var OwnerForm = this;



    var width = config.Width, height = config.Height;
    config.Width = 328;
    config.Height = 248;

    Control.call(This, config);

    var Base = {
        GetType: This.GetType,
        is: This.is
    };

    This.GetType = function () { return "AddFriendForm"; }
    This.is = function (type) { return type == This.GetType() ? true : Base.is(type); }

    var m_Tab = new Controls.SimpleTabControl({ "Left": 1, "Top": 1, "Width": 326, "Height": 246, "AnchorStyle": Controls.AnchorStyle.Left | Controls.AnchorStyle.Right | Controls.AnchorStyle.Top | Controls.AnchorStyle.Bottom, "Parent": This, "Text": "", "Css": "simple_tab_gred", "Tabs": [{ "Text": "添加好友/群", "Width": 120, "ID": "ID100000007", "IsSelected": true}], "BorderWidth": 1 });



    m_Tab.OnSelectedTab.Attach(
        function (index, preIndex) {

        }
    )


    var label2 = new Controls.Label({ "Left": 12, "Top": 20, "Width": 95, "Height": 14, "AnchorStyle": Controls.AnchorStyle.Left | Controls.AnchorStyle.Top, "Parent": m_Tab.GetPanel(0), "Text": "群/用户账户名：", "Css": "label" });




    var m_EditNameF = new Controls.TextBox({ "Left": 112, "Top": 16, "Width": 202, "Height": 22, "AnchorStyle": Controls.AnchorStyle.Left | Controls.AnchorStyle.Right | Controls.AnchorStyle.Top, "Parent": m_Tab.GetPanel(0), "Text": "", "Css": "textbox", "BorderWidth": 1 });

    if (Core.Params["Name"] != undefined) m_EditNameF.SetText(Core.Params["Name"]);


    var label4 = new Controls.Label({ "Left": 11, "Top": 54, "Width": 139, "Height": 14, "AnchorStyle": Controls.AnchorStyle.Left | Controls.AnchorStyle.Top, "Parent": m_Tab.GetPanel(0), "Text": "验证信息：", "Css": "label" });




    var m_EditInfoF = new Controls.TextArea({ "Left": 11, "Top": 74, "Width": 302, "Height": 102, "AnchorStyle": Controls.AnchorStyle.Left | Controls.AnchorStyle.Right | Controls.AnchorStyle.Top | Controls.AnchorStyle.Bottom, "Parent": m_Tab.GetPanel(0), "Text": "", "Css": "textarea", "BorderWidth": 1 });




    var m_BtnAddFriend = new Controls.Button({ "Left": 249, "Top": 184, "Width": 64, "Height": 26, "AnchorStyle": Controls.AnchorStyle.Right | Controls.AnchorStyle.Bottom, "Parent": m_Tab.GetPanel(0), "Text": "确定", "Css": "button_default" });



    m_BtnAddFriend.OnClick.Attach(
        function (btn) {
            Common.SendAddFriendRequest(
            function (result, ex) {
                if (!result) {
                    Core.Utility.ShowError(ex.toString());
                }
                else {
                    m_EditNameF.SetText("");
                    m_EditInfoF.SetText("")
                    Core.Utility.ShowFloatForm("添加好友的请求已发送，等待对方确认...", "text");
                }
            },
            m_EditNameF.GetText(), m_EditInfoF.GetText());
        }
    )

    This.Resize(width, height);



}


Module.FriendManagementForm = FriendManagementForm;

function FriendManagementForm(config) {
    var This = this;
    var OwnerForm = this;



    var width = config.Width, height = config.Height;
    config.Width = 596;
    config.Height = 502;

    Control.call(This, config);

    var Base = {
        GetType: This.GetType,
        is: This.is
    };

    This.GetType = function () { return "FriendManagementForm"; }
    This.is = function (type) { return type == This.GetType() ? true : Base.is(type); }

    var tab2 = new Controls.SimpleTabControl({ "Left": 1, "Top": 1, "Width": 594, "Height": 500, "AnchorStyle": Controls.AnchorStyle.Left | Controls.AnchorStyle.Right | Controls.AnchorStyle.Top | Controls.AnchorStyle.Bottom, "Parent": This, "Text": "", "Css": "simple_tab_gred", "Tabs": [{ "Text": "好友管理", "Width": 80, "ID": "ID100000010", "IsSelected": true }, { "Text": "群管理", "Width": 80, "ID": "ID100000011", "IsSelected": false }, { "Text": "所有用户", "Width": 80, "ID": "ID100000012", "IsSelected": false }, { "Text": "所有群组", "Width": 80, "ID": "ID100000013", "IsSelected": false}], "BorderWidth": 1 });



    tab2.OnSelectedTab.Attach(
        function (index, preIndex) {
            if (index == 0) {
                CurrentWindow.Waiting("正在载入好友列表...");
                m_FrameFriend.Navigate(String.format("FriendList.aspx?random={0}", (new Date()).getTime()));
            }
            else if (index == 1) {
                CurrentWindow.Waiting("正在载入群列表...");
                m_FrameGroup.Navigate(String.format("GroupList.aspx?random={0}", (new Date()).getTime()));

            }
            else if (index == 2) {
                CurrentWindow.Waiting("正在载入用户列表...");
                m_FrameAllUsers.Navigate(String.format("AllUSers.aspx?random={0}", (new Date()).getTime()));

            }
            else if (index == 3) {
                CurrentWindow.Waiting("正在载入群列表...");
                m_FrameAllGroups.Navigate(String.format("AllGroups.aspx?random={0}", (new Date()).getTime()));

            }
        }
    )



    var m_FrameFriend_Config = { "Left": 6, "Top": 6, "Width": 580, "Height": 462, "AnchorStyle": Controls.AnchorStyle.Left | Controls.AnchorStyle.Right | Controls.AnchorStyle.Top | Controls.AnchorStyle.Bottom, "Parent": tab2.GetPanel(0), "Text": "", "Css": "frame" };

    m_FrameFriend_Config.BorderWidth = 1;

    var m_FrameFriend = new Controls.Frame(m_FrameFriend_Config);



    m_FrameFriend.OnResized.Attach(
        function (btn) {

        }
    )



    var m_FrameGroup_Config = { "Left": 6, "Top": 6, "Width": 580, "Height": 462, "AnchorStyle": Controls.AnchorStyle.Left | Controls.AnchorStyle.Right | Controls.AnchorStyle.Top | Controls.AnchorStyle.Bottom, "Parent": tab2.GetPanel(1), "Text": "", "Css": "frame" };

    m_FrameGroup_Config.BorderWidth = 1;

    var m_FrameGroup = new Controls.Frame(m_FrameGroup_Config);



    m_FrameGroup.OnResized.Attach(
        function (btn) {

        }
    )



    var m_FrameAllUsers_Config = { "Left": 6, "Top": 6, "Width": 580, "Height": 462, "AnchorStyle": Controls.AnchorStyle.Left | Controls.AnchorStyle.Right | Controls.AnchorStyle.Top | Controls.AnchorStyle.Bottom, "Parent": tab2.GetPanel(2), "Text": "", "Css": "frame" };

    m_FrameAllUsers_Config.BorderWidth = 1;

    var m_FrameAllUsers = new Controls.Frame(m_FrameAllUsers_Config);



    m_FrameAllUsers.OnResized.Attach(
        function (btn) {

        }
    )



    var m_FrameAllGroups_Config = { "Left": 6, "Top": 6, "Width": 580, "Height": 462, "AnchorStyle": Controls.AnchorStyle.Left | Controls.AnchorStyle.Right | Controls.AnchorStyle.Top | Controls.AnchorStyle.Bottom, "Parent": tab2.GetPanel(3), "Text": "", "Css": "frame" };

    m_FrameAllGroups_Config.BorderWidth = 1;

    var m_FrameAllGroups = new Controls.Frame(m_FrameAllGroups_Config);



    m_FrameAllGroups.OnResized.Attach(
        function (btn) {

        }
    )

    This.Resize(width, height);

    tab2.Select(0);

    tab2.SetTabVisible(2, Core.Session.GetUserName().toLowerCase() == "sa");
    tab2.SetTabVisible(3, Core.Session.GetUserName().toLowerCase() == "sa");

    CurrentWindow.OnNotify.Attach(
    function (command, data) {
        if (command == "FriendInfoChanged") {
            if (tab2.GetSelectedIndex() == 0) {
                CurrentWindow.Waiting("正在载入好友列表...");
                m_FrameFriend.Navigate(String.format("FriendList.aspx?random={0}", (new Date()).getTime()));
            }
            else if (tab2.GetSelectedIndex() == 1) {
                CurrentWindow.Waiting("正在载入群列表...");
                m_FrameGroup.Navigate(String.format("GroupList.aspx?Type=1&random={0}", (new Date()).getTime()));
            }
        }
    });

}


Module.ImageControl = ImageControl;

function ImageControl(config) {
    var This = this;
    var OwnerForm = this;

    config.Css = "ImageControl";

    var width = config.Width, height = config.Height;
    config.Width = 200;
    config.Height = 200;

    Control.call(This, config);

    var Base = {
        GetType: This.GetType,
        is: This.is
    };

    This.GetType = function () { return "ImageControl"; }
    This.is = function (type) { return type == This.GetType() ? true : Base.is(type); }

    This.Resize(width, height);

    var img = null;

    function centerImage() {
        if (img.width > config.ImageWidth || img.height > config.ImageHeight) {
            if (img.width * config.ImageHeight > img.height * config.ImageWidth) {
                img.height = config.ImageWidth * img.height / img.width;
                img.width = config.ImageWidth;
            }
            else {
                img.width = config.ImageHeight * img.width / img.height;
                img.height = config.ImageHeight;
            }
        }
        img.style.marginLeft = (This.GetClientWidth() - img.width) / 2 + 'px';
        img.style.marginTop = (This.GetClientHeight() - img.height) / 2 + 'px';
    }

    This.OnResized.Attach(
    function () {
        if (img != null) {
            img.style.marginLeft = (This.GetClientWidth() - img.width) / 2 + 'px';
            img.style.marginTop = (This.GetClientHeight() - img.height) / 2 + 'px';
        }
    });

    This.LoadImage = function (src) {
        This.GetDom().innerHTML = "<img/>";
        img = This.GetDom().firstChild;
        img.onload = centerImage;
        img.src = src;
    }

    This.GetImageSrc = function () {
        return img == null ? "" : img.src;
    }

}

function UserInfoPanel(config)
{ }

function GroupInfoPanel(config)
{ }

Module.UpdateSelfInfo = UpdateSelfInfo;

function UpdateSelfInfo(config)
{ }

Module.UpdateGroupInfo = UpdateGroupInfo;

function UpdateGroupInfo(config)
{ }
